﻿@using System.Web.Script.Serialization
@{
    ViewBag.Title = T("Register.Title");
}
<p class="text-error" data-bind="visible:ErrorMessage != null,text:ErrorMessage"> </p>

<h2>@T("Register.Title")</h2>


<label>@T("Register.UserName")</label>
<input type="text" data-bind="value:UserName,attr:{placeholder:'@T("Register.UserName")'}"/>
  <span data-bind="validationMessage: UserName" class="text-error"></span>

<label>@T("Register.Email")</label>
<input type="text" data-bind="value:Email,attr:{placeholder:'@T("Register.Email")'}"/>
  <span data-bind="validationMessage: Email" class="text-error"></span>

<label>@T("Register.Password")</label>
<input type="password" data-bind="value:Password,attr:{placeholder:'@T("Register.Password")'}"/>
  <span data-bind="validationMessage: Password" class="text-error"></span>

 <div>
<input type="button"  data-bind="value:'@T("Register.Register")',click:Regist"  class="btn btn-primary"/>

     </div>
@{
    var model = ViewBag.Model;

    var jsonStr = new HtmlString(new JavaScriptSerializer().Serialize(model));
}

@section scripts{
    <script>
        var init = @jsonStr;


        function RegisterModel() {
            var self = this;
            self.UserName = ko.observable(init.UserName).extend({
                required: { message: '@T("Register.NeedUserName")' },
            });
            self.Password = ko.observable(init.Password).extend({
                required: { message: '@T("Register.NeedPassword")' },
            });
            self.Email = ko.observable(init.Email).extend({
                required: { message: '@T("Register.NeedEmailAddress")' },
            });

            
            self.ErrorMessage = ko.observable(null);


            self.Regist = function() {
                self.ErrorMessage(null);

                if (this.isValid()) {
                    $.ajax({
                        url: "/api/RegisterApi/Regist",
                        type: 'post',
                        data: ko.toJSON(self),
                        contentType: 'application/json',
                        success: function(result) {

                            if (result.Status == "0") {
                                location.href = result.RedirectUrl;
                            } else {
                                self.ErrorMessage(result.Message);
                            }
                        }
                    });
                } else {
                    self.errors.showAllMessages();
                }
            };
        }

        var vm = new RegisterModel();
        vm.errors = ko.validation.group(vm);
        ko.applyBindings(vm);
    </script>
}